Inside Macintosh: Memory

Previous | Chapter Top | Chapter Contents | Next

Freeing Memory

The Memory Manager compacts and purges the heap whenever necessary to satisfy requests for memory. You can also compact or purge the heap manually. To compact the current heap zone manually, use the CompactMem function. To purge it manually, use the PurgeMem procedure. To do both at once, use the MaxMem function. To perform the same operations on the system heap zone, use the CompactMemSys function, the PurgeMemSys procedure, and the MaxMemSys function.

Most applications don't need to call the routines described in this section. Normally you should let the Memory Manager compact or purge your application heap.

CompactMem

The Memory Manager compacts the heap for you when you make a memory request that it can't fill. However, you can use the CompactMem function to compact the current heap zone manually.

FUNCTION CompactMem (cbNeeded: Size): Size;
cbNeeded
The size, in bytes, of the block for which CompactMem should attempt to make room.

DESCRIPTION

The CompactMem function compacts the current heap zone by moving unlocked, relocatable blocks down until they encounter nonrelocatable blocks or locked, relocatable blocks, but not by purging blocks. It continues compacting until it either finds a contiguous block of at least cbNeeded free bytes or has compacted the entire zone.

The CompactMem function returns the size, in bytes, of the largest contiguous free block for which it could make room, but it does not actually allocate that block.

To compact the entire heap zone, call CompactMem(maxSize) . The Memory Manager defines the constant maxSize for the largest contiguous block possible in the 24-bit Memory Manager:

CONST
    maxSize                 = $800000;              {maximum size of a block}

SPECIAL CONSIDERATIONS

Because CompactMem moves memory, you should not call it at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for CompactMem are

Registers on entry

D0

Size of block to make room for

Registers on exit

D0

Size of largest allocatable block

The CompactMem function compacts the current heap zone. If you want to compact the system heap zone rather than the current heap zone, set bit 10 of the routine trap word. In most development systems, you can do this by supplying the word SYS as the second argument to the routine macro, as follows:

_CompactMem ,SYS

RESULT CODES

noErr

0

No error

CompactMemSys

You can use the CompactMemSys function to compact the system heap zone manually.

FUNCTION CompactMemSys (cbNeeded: Size): Size;
cbNeeded
The size in bytes of the block for which CompactMemSys should attempt to make room.

DESCRIPTION

The CompactMemSys function works much as the CompactMem function does, but compacts the system heap instead of the current heap.

RESULT CODES

noErr

0

No error

PurgeMem

The Memory Manager purges the heap for you when you make a memory request that it can't fill. However, you can use the PurgeMem procedure to purge the current heap zone manually.

PROCEDURE PurgeMem (cbNeeded: Size);
cbNeeded
The size, in bytes, of the block for which PurgeMem should attempt to make room.

DESCRIPTION

The PurgeMem procedure sequentially purges blocks from the current heap zone until it either allocates a contiguous block of at least cbNeeded free bytes or has purged the entire zone. If it purges the entire zone without creating a contiguous block of at least cbNeeded free bytes, PurgeMem generates a memFullErr .

The PurgeMem procedure purges only relocatable, unlocked, purgeable blocks.

The PurgeMem procedure does not actually attempt to allocate a block of cbNeeded bytes.

To purge the entire heap zone, call PurgeMem(maxSize) .

SPECIAL CONSIDERATIONS

Because PurgeMem purges memory, you should not call it at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for PurgeMem are

Registers on entry

D0

Size of block to make room for

Registers on exit

D0

Result code

The PurgeMem procedure purges the current heap zone. If you want to purge the system heap zone rather than the current heap zone, set bit 10 of the routine trap word. In most development systems, you can do this by supplying the word SYS as the second argument to the routine macro, as follows:

_PurgeMem ,SYS

RESULT CODES

noErr

0

No error

memFullErr

-108

Not enough memory

PurgeMemSys

You can use the PurgeMemSys procedure to purge the system heap manually.

PROCEDURE PurgeMemSys (cbNeeded: Size);
cbNeeded
The size, in bytes, of the block for which PurgeMemSys should attempt to make room.

DESCRIPTION

The PurgeMemSys procedure works much as the PurgeMem procedure does, but purges the system heap instead of the current heap.

RESULT CODES

noErr

0

No error

memFullErr

-108

Not enough memory

MaxMem

Use the MaxMem function to compact and purge the current heap zone.

FUNCTION MaxMem (VAR grow: Size): Size;
grow
On exit, the maximum number of bytes by which the current heap zone can grow. After a call to MaxApplZone , MaxMem always returns 0 in this parameter.

DESCRIPTION

The MaxMem function compacts the current heap zone and purges all relocatable, unlocked, and purgeable blocks from the zone. It returns the size, in bytes, of the largest contiguous free block in the zone after the compacting and purging. If the current zone is the original application zone, the grow parameter is set to the maximum number of bytes by which the zone can grow. For any other heap zone, grow is set to 0. MaxMem doesn't actually expand the zone or call the zone's grow-zone function.

SPECIAL CONSIDERATIONS

Because MaxMem moves and purges memory, you should not call it at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The registers on exit for MaxMem are

Registers on exit

A0

Number of bytes zone can grow

D0

Size in bytes of largest allocatable block

The MaxMem function compacts the current heap zone. If you want to compact and purge the system heap zone rather than the current heap zone, set bit 10 of the routine trap word. In most development systems, you can do this by supplying the word SYS as the second argument to the routine macro, as follows:

_MaxMem ,SYS

RESULT CODES

noErr

0

No error

MaxMemSys

You can use the MaxMemSys function to purge and compact the system heap zone manually.

FUNCTION MaxMemSys (VAR grow: Size): Size;
grow
On exit, the MaxMemSys function sets this parameter to 0. Ignore this parameter.

DESCRIPTION

The MaxMemSys function works much as the MaxMem function does, but compacts and purges the system heap instead of the current heap. It returns the size, in bytes, of the largest block you can allocate in the system heap.

RESULT CODES

noErr

0

No error


© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next